#include "friendship_service.h"

// 状态常量定义
#define FRIEND_PENDING 0
#define FRIEND_ACCEPTED 1
#define FRIEND_REJECTED 2

// 创建好友关系
int create_friendship(sqlite3 *db, int user_id1, int user_id2) {
    // 确保 user_id1 < user_id2
    int min_id = (user_id1 < user_id2) ? user_id1 : user_id2;
    int max_id = (user_id1 > user_id2) ? user_id1 : user_id2;
    
    char *sql = "INSERT INTO friendships (user_id1, user_id2, status) "
                "VALUES (?, ?, ?);";
    
    sqlite3_stmt *stmt;
    int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
        return -1;
    }
    
    sqlite3_bind_int(stmt, 1, min_id);
    sqlite3_bind_int(stmt, 2, max_id);
    sqlite3_bind_int(stmt, 3, FRIEND_PENDING);
    
    rc = sqlite3_step(stmt);
    if (rc != SQLITE_DONE) {
        fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
        sqlite3_finalize(stmt);
        return -1;
    }
    
    sqlite3_finalize(stmt);
    return sqlite3_last_insert_rowid(db);
}

// 更新好友关系状态
int update_friendship_status(sqlite3 *db, int user_id1, int user_id2, int new_status) {
    // 确保 user_id1 < user_id2
    int min_id = (user_id1 < user_id2) ? user_id1 : user_id2;
    int max_id = (user_id1 > user_id2) ? user_id1 : user_id2;
    
    char *sql = "UPDATE friendships SET status = ? "
                "WHERE user_id1 = ? AND user_id2 = ?;";
    
    sqlite3_stmt *stmt;
    int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
        return -1;
    }
    
    sqlite3_bind_int(stmt, 1, new_status);
    sqlite3_bind_int(stmt, 2, min_id);
    sqlite3_bind_int(stmt, 3, max_id);
    
    rc = sqlite3_step(stmt);
    if (rc != SQLITE_DONE) {
        fprintf(stderr, "Update failed: %s\n", sqlite3_errmsg(db));
        sqlite3_finalize(stmt);
        return -1;
    }
    
    int rows_affected = sqlite3_changes(db);
    sqlite3_finalize(stmt);
    return rows_affected;
}

// 获取用户的所有好友关系 ****需要free(friendships);
int get_user_friendships(sqlite3 *db, int user_id, int **friendships, int *count) {
    char *sql = "SELECT friendship_id, user_id1, user_id2, status, "
                "strftime('%s', created_at), strftime('%s', updated_at) "
                "FROM friendships "
                "WHERE user_id1 = ? OR user_id2 = ?;";
    
    sqlite3_stmt *stmt;
    int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
        return -1;
    }
    
    sqlite3_bind_int(stmt, 1, user_id);
    sqlite3_bind_int(stmt, 2, user_id);
    
    // 第一次遍历获取结果数量
    *count = 0;
    while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
        (*count)++;
    }
    
    if (rc != SQLITE_DONE) {
        fprintf(stderr, "Query failed: %s\n", sqlite3_errmsg(db));
        sqlite3_finalize(stmt);
        return -1;
    }
    
    // 重置查询
    sqlite3_reset(stmt);
    
    // 分配内存
    *friendships = (int *)malloc(*count * 6 * sizeof(int));
    if (*friendships == NULL) {
        fprintf(stderr, "Memory allocation failed\n");
        sqlite3_finalize(stmt);
        return -1;
    }
    
    // 获取结果
    int index = 0;
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        (*friendships)[index++] = sqlite3_column_int(stmt, 0); // friendship_id
        (*friendships)[index++] = sqlite3_column_int(stmt, 1); // user_id1
        (*friendships)[index++] = sqlite3_column_int(stmt, 2); // user_id2
        (*friendships)[index++] = sqlite3_column_int(stmt, 3); // status
        (*friendships)[index++] = sqlite3_column_int(stmt, 4); // created_at
        (*friendships)[index++] = sqlite3_column_int(stmt, 5); // updated_at
    }
    
    sqlite3_finalize(stmt);
    return 0;
}

// 删除好友关系
int delete_friendship(sqlite3 *db, int friendship_id) {
    char *sql = "DELETE FROM friendships WHERE friendship_id = ?;";
    
    sqlite3_stmt *stmt;
    int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
        return -1;
    }
    
    sqlite3_bind_int(stmt, 1, friendship_id);
    
    rc = sqlite3_step(stmt);
    if (rc != SQLITE_DONE) {
        fprintf(stderr, "Delete failed: %s\n", sqlite3_errmsg(db));
        sqlite3_finalize(stmt);
        return -1;
    }
    
    int rows_affected = sqlite3_changes(db);
    sqlite3_finalize(stmt);
    return rows_affected;
}

// 检查好友关系是否存在
int friendship_exists(sqlite3 *db, int user_id1, int user_id2) {
    // 确保 user_id1 < user_id2
    int min_id = (user_id1 < user_id2) ? user_id1 : user_id2;
    int max_id = (user_id1 > user_id2) ? user_id1 : user_id2;
    
    char *sql = "SELECT COUNT(*) FROM friendships "
                "WHERE user_id1 = ? AND user_id2 = ?;";
    
    sqlite3_stmt *stmt;
    int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
        return -1;
    }
    
    sqlite3_bind_int(stmt, 1, min_id);
    sqlite3_bind_int(stmt, 2, max_id);
    
    rc = sqlite3_step(stmt);
    if (rc != SQLITE_ROW) {
        fprintf(stderr, "Query failed: %s\n", sqlite3_errmsg(db));
        sqlite3_finalize(stmt);
        return -1;
    }
    
    int count = sqlite3_column_int(stmt, 0);
    sqlite3_finalize(stmt);
    return count;
}